package com.jz.community.moduleshopping.orderDetail.ui;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.basecomm.utils.rxbus.RxBus;
import com.jz.community.commview.view.MyRatingBar;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.orderDetail.bean.EvaluateInfo;
import com.jz.community.moduleshopping.orderDetail.bean.OrderEvent;
import com.jz.community.moduleshopping.orderDetail.task.OrderEvaluateDetailTask;

import butterknife.BindView;

/**
 * Created by Tony on 2018/5/7.
 * 评价
 */
public class OrderEvaluateActivity extends BaseMvpActivity {
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.ratingbar_goods)
    MyRatingBar ratingbarGoods;
    @BindView(R2.id.ratingbar_service)
    MyRatingBar ratingbarService;
    @BindView(R2.id.edit_reply)
    EditText editReply;
    private String orderId;
    private String shopId;
    private float score_goods;
    private float score_service;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_activity_comments;
    }

    @Override
    protected void initView() {
        TextView tv = findViewById(R.id.title_name);
        tv.setText("订单评价");
        setImmersionBar(titleToolbar);
        orderId = getIntent().getStringExtra("orderId");
        shopId = getIntent().getStringExtra("shopId");
    }

    @Override
    protected void addListener() {
        ratingbarGoods.setOnRatingChangeListener(new MyRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                score_goods = RatingCount;
            }
        });
        ratingbarService.setOnRatingChangeListener(new MyRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                score_service = RatingCount;
            }
        });
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.button_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitEvaluateMsg();
            }
        });
    }

    //提交评价信息
    private void submitEvaluateMsg() {
        String info = editReply.getText().toString().trim();
        if (score_goods == 0) {
            WpToast.l(this, "给商品评个分吧~");
            return;
        }
        if (score_service == 0) {
            WpToast.l(this, "给商家服务评个分吧~");
            return;
        }

        if (Preconditions.isNullOrEmpty(info)) {
            WpToast.l(this, "评价内容不能为空~");
            return;
        }

        new OrderEvaluateDetailTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                EvaluateInfo evaluateInfo = (EvaluateInfo) o;
                if (Preconditions.isNullOrEmpty(evaluateInfo)) {
                    return;
                }
                if (!Preconditions.isNullOrEmpty(evaluateInfo.getOrderCommentContent())) {
                    WpToast.l(OrderEvaluateActivity.this, "评价成功");
                    handleRxEventPost();
                    finish();
                } else {
                    if (!Preconditions.isNullOrEmpty(evaluateInfo.getMessage())) {
                        WpToast.l(OrderEvaluateActivity.this, evaluateInfo.getMessage());
                    } else {
                        WpToast.l(OrderEvaluateActivity.this, "评价失败");
                    }
                }
            }
        }).execute(ConverterUtils.toString(orderId), info, ConverterUtils.toString(score_goods), ConverterUtils.toString(score_service), shopId);
    }

    /**
     * 发送消息更新订单列表
     */
    public void handleRxEventPost() {
        OrderEvent orderEvent = new OrderEvent();
        orderEvent.setOrderType(OrderEvent.ORDER_TYPE_1);
        RxBus.getInstance().post(orderEvent);
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

}
